/*
 * 8.1.cpp
 *
 *  Created on: 2012-5-18
 *      Author: jawinton
 */

#include <iostream>
#include <cstring>
#include <cstdio>
#include <cstdlib>

using namespace std;

#define MAX 21252

int main (void) {
	int p, e, i, d;
	scanf("%d%d%d%d", &p, &e, &i, &d);
	for (int j=1; p>=0; j++,scanf("%d%d%d%d", &p, &e, &i, &d)) {
		int day;
		for (day=d+1; (day-p)%23!=0 && day<=MAX; day++);
		for (; (day-e)%28!=0 && day<=MAX; day+=23);
		for (; (day-i)%33!=0 && day<=MAX; day+=23*28);
		cout << "Case " << j <<": the next triple peak occurs in " << (day-d) << " days" << endl;
	}
	return 0;
}
